﻿using K8.EF;
using KingerRobot.PCRMES.Dao;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace KingerRobot.PCRMES.SpiService;

public class WmsOutRepositories: IWmsOutRepositories
{
    #region 同步方法
    public i_wms_out QueryById(string id)
    {
        var _db = Db.Default;
        return _db.WmsOuts.FirstOrDefault(d => d.OUT_ID == id);
    }

    /// <summary>
    /// 功能描述:根据ID查询数据
    /// 作　　者:Robin
    /// </summary>
    /// <param name="lstIds">id列表,如果是联合主键，请使用Where条件</param>
    /// <returns>数据实体列表</returns>
    public List<i_wms_out> QueryByIDs(object[] lstIds)
    {
        var _db = Db.Default;
        return _db.WmsOuts.Where(d => lstIds.Contains(lstIds)).ToList();
    }
    /// <summary>
    /// 写入实体数据
    /// </summary>
    /// <param name="entity">博文实体类</param>
    /// <returns>插入个数 插入成功为1</returns>
    public int Add(i_wms_out entity)
    {
        var _db = Db.Default;
        return _db.WmsOuts.Insert(entity);
    }

    /// <summary>
    /// 批量插入实体
    /// </summary>
    /// <param name="listEntity">实体集合</param>
    /// <returns>影响行数</returns>
    public int Add(IEnumerable<i_wms_out> listEntity)
    {
        var _db = Db.Default;
        _db.WmsOuts.AddRange(listEntity);
        int insertedCount = _db.SaveChanges();
        return insertedCount;
    }
    /// <summary>
    /// 根据实体删除一条数据
    /// </summary>
    /// <param name="entity">博文实体类</param>
    /// <returns></returns>
    public int DeleteById(string id)
    {
        var _db = Db.Default;
        return _db.WmsOuts.Where(d => d.OUT_ID == id).Delete();
    }

    /// <summary>
    /// 更新实体数据
    /// </summary>
    /// <param name="entity">博文实体类</param>
    /// <returns></returns>
    public int Update(i_wms_out entity)
    {
        var _db = Db.Default;
        return _db.WmsOuts.Update<i_wms_out>(new i_wms_out()
        {
            OUT_ID = entity.OUT_ID,
            LIST_NO = entity.LIST_NO,
            PART_NUMBER = entity.PART_NUMBER,
            BATCH_NAME = entity.BATCH_NAME,
            ELOC_NO = entity.ELOC_NO,
            RFID_NO = entity.RFID_NO,
            CREATE_TIME = entity.CREATE_TIME,
            CREATE_USER = entity.CREATE_USER,
            LAST_MODIFIED_TIME = entity.LAST_MODIFIED_TIME,
            LAST_MODIFIED_USER = entity.LAST_MODIFIED_USER,
            REMARKS = entity.REMARKS,
            UPDATE_FLAG = entity.UPDATE_FLAG,
            AREA_NAME = entity.AREA_NAME,
            STORAGE_NO = entity.STORAGE_NO,
            TARGET_EQUIP = entity.TARGET_EQUIP,
            OUT_FLAG = entity.OUT_FLAG,
            PRINT_TIME = entity.PRINT_TIME,
            OUT_TIME = entity.OUT_TIME,
            PART_DESC = entity.PART_DESC,
            LIST_STATUS = entity.LIST_STATUS,
            DATA_TYPE = entity.DATA_TYPE,
            QUANTITY = entity.QUANTITY,
        });
    }
    /// <summary>
    /// 待条件更新所有数据
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <param name="entity"></param>
    /// <returns></returns>
    public int Update(Expression<Func<i_wms_out, bool>> whereExpression, i_wms_out entity)
    {
        var _db = Db.Default;
        return _db.WmsOuts.Where(whereExpression).Update(new i_wms_out()
        {
            OUT_ID = entity.OUT_ID,
            LIST_NO = entity.LIST_NO,
            PART_NUMBER = entity.PART_NUMBER,
            BATCH_NAME = entity.BATCH_NAME,
            ELOC_NO = entity.ELOC_NO,
            RFID_NO = entity.RFID_NO,
            CREATE_TIME = entity.CREATE_TIME,
            CREATE_USER = entity.CREATE_USER,
            LAST_MODIFIED_TIME = entity.LAST_MODIFIED_TIME,
            LAST_MODIFIED_USER = entity.LAST_MODIFIED_USER,
            REMARKS = entity.REMARKS,
            UPDATE_FLAG = entity.UPDATE_FLAG,
            AREA_NAME = entity.AREA_NAME,
            STORAGE_NO = entity.STORAGE_NO,
            TARGET_EQUIP = entity.TARGET_EQUIP,
            OUT_FLAG = entity.OUT_FLAG,
            PRINT_TIME = entity.PRINT_TIME,
            OUT_TIME = entity.OUT_TIME,
            PART_DESC = entity.PART_DESC,
            LIST_STATUS = entity.LIST_STATUS,
            DATA_TYPE = entity.DATA_TYPE,
            QUANTITY = entity.QUANTITY,
        });
    }

    /// <summary>
    /// 正序查询第一条数据
    /// </summary>
    /// <returns></returns>
    public i_wms_out First()
    {
        var _db = Db.Default;
        return _db.WmsOuts.First();
    }
    /// <summary>
    /// 查询第一条数据
    /// </summary>
    /// <param name="whereExpression"></param>
    /// <returns></returns>
    public i_wms_out First(Expression<Func<i_wms_out, bool>> whereExpression)
    {
        var _db = Db.Default;
        if (whereExpression == null) return null;
        return _db.WmsOuts.Where(whereExpression).FirstOrDefault();
    }


    /// <summary>
    /// 功能描述:查询所有数据
    /// 作　　者:Robin
    /// </summary>
    /// <returns>数据列表</returns>
    public List<i_wms_out> Query()
    {
        try
        {
            var _db = Db.Default;
            return _db.WmsOuts.ToList();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Query执行异常:{ex.Message}");
        }
        return null;
    }


    /// <summary>
    /// 功能描述:查询数据列表
    /// 作　　者:Robin
    /// </summary>
    /// <param name="whereExpression">whereExpression</param>
    /// <returns>数据列表</returns>
    public List<i_wms_out> Query(Expression<Func<i_wms_out, bool>> whereExpression)
    {
        var _db = Db.Default;
        if (whereExpression == null) return null;
        return _db.WmsOuts.Where(whereExpression).ToList();
    }
    #endregion
}
